<template>
  <div id="login">
    <h2>登录页面</h2>
    <el-form ref="form" :model="form" label-width="20%">
      <el-form-item label="用户名:">
        <el-input v-model="form.account" autocomplete="on" name="用户名"></el-input>
      </el-form-item>
      <el-form-item label="密  码:">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>

      <el-radio v-model="form.userType" label="0">普通用户</el-radio>
      <el-radio v-model="form.userType" label="1">物流人员</el-radio>
    </el-form>
    <el-button type="primary" round @click="login" class="btn">登录</el-button>
    <router-link to="register">前往注册</router-link>
  </div>
</template>

<script>
import { LoginAPI } from "@/request/api.js";
import { setToken } from "@/utils/auth.js";
export default {
  data() {
    return {
      form: {
        account: "",
        password: "",
        userType: "",
      },
    };
  },

  methods: {
    login() {
      if (this.form.account == "") {
        this.$message.error("用户名不能为空");
      } else if (this.form.password == "") {
        this.$message.error("密码不能为空");
      } else if (this.form.userType === "") {
        this.$message.error("请选择用户类型");
      } else {
        LoginAPI(this.form).then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            setToken(res.data.data);
            if (this.form.userType == 1) {
              this.$router.push({
                path: "/adFrontpage",
              });
            } else {
              this.$router.push({
                path: "/frontpage",
              });
            }
          } else {
            this.$alert(res.data.msg, "登录失败", {
              confirmButtonText: "确定",
              callback: () => {
                (this.form.account = ""), (this.form.password = "");
              },
            });
          }
        });
      }
    },
  },
};
</script>

<style scoped>
#login {
  height: 350px;
  width: 350px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.25);
  background: #ffffff;
  opacity: 0.85;
  position: absolute;
  top: 20%;
  left: 35%;
  text-align: center;
  display: flex;
  flex-direction: column; /*横向*/
  padding: 30px;
}
#login h2 {
  padding-bottom: 30px;
}
.btn {
  width: 60%;
  margin: auto;
}
.reg {
  color: red;
  position: absolute;
  bottom: 10px;
  right: 10px;
  cursor: pointer;
}
</style>